/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screen;

import ClassFrame.Button;
import ClassFrame.InputKey;
import ClassFrame.Map;
import ClassFrame.Popup;
import ClassFrame.PopupOnclickListener;
import ClassFrame.Resource;
import GamePlay.CanvasGame;
import GamePlay.MIDletGame;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author QuyetNM1
 */
public class ChooseLevel extends IScreen implements PopupOnclickListener {

    private long startTime;
    private long currTime;
    private int chosenLeveli = 0;
    private int chosenLevelj = 0;
    public Popup popup, popup1;

    public ChooseLevel(CanvasGame _canvas) {
        super(_canvas);
    }

    public void initComponents() {
        canvas.getMap().initLevelMap();
//        canvas.getMap().getlArray()[0][0].setState(Button.HOLD_CLICK);
        popup = new Popup(1, canvas, this, Resource.IMG_BG_NOTICE, "Để mở thêm 3 level bạn sẽ phải trả " + MIDletGame.PRICE + ". Bạn có muốn thanh toán không?", "Mua", "Không");
        popup1 = new Popup(2, canvas, this, Resource.IMG_BG_NOTICE, "Bạn cần chơi qua level trước để mở level này!", "Đồng ý", null);
    }

    public void load_screen() {
        canvas.getResource().loadArray(Resource.chooseLevelArrayImages);
    }

    public void un_load_screen() {
        canvas.getResource().unLoadArray(Resource.chooseLevelArrayImages);
    }

    public void keyEvent(int keycode, int event) {
        switch (event) {
            case InputKey.KEY_EVENT_DOWN:

                break;
            case InputKey.KEY_UP:

                break;
            case InputKey.KEY_DOWN:
                break;
            case InputKey.KEY_RIGHT:
                break;
            case InputKey.KEY_LEFT:

                break;
        }
    }

    public void pointerEvent(int x, int y, int event) {
        if (popup.isShowing()) {
            popup.pointerEvent(x, y, event);
        } else if (popup1.isShowing()) {
            popup1.pointerEvent(x, y, event);
        } else {
            switch (event) {
                case InputKey.POINTER_EVENT_DOWN:
                    for (int i = 0; i < canvas.getMap().getlArray().length; i++) {
                        for (int j = 0; j < canvas.getMap().getlArray()[i].length; j++) {
                            if (canvas.getMap().getlArray()[i][j].isClick(x, y)) {
                                canvas.getMap().getlArray()[i][j].setState(Button.HOLD_CLICK);
                                canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                                break;
                            }
                        }
                    }
                    break;
                case InputKey.POINTER_EVENT_UP:
                    for (int i = 0; i < canvas.getMap().getlArray().length; i++) {
                        for (int j = 0; j < canvas.getMap().getlArray()[i].length; j++) {
                            if (canvas.getMap().getlArray()[i][j].isClick(x, y) && canvas.getMap().getlArray()[i][j].getState() == Button.HOLD_CLICK) {
                                int level = j * canvas.getMap().getlArray().length + i;
                                if (level <= Map.levelMax) {
                                    Map.level = level;
                                    System.out.println("Level = " + Map.level);
                                    canvas.getaPlayer().setLevel(Map.level);
                                    canvas.setScreen(new Welcome2(canvas));
//                                    canvas.getMIDletGame().getTracker().addToQueue(new PageView("DuoiHinhBatChu_Level" + (Map.level+1)));
//                                    canvas.getMIDletGame().getTracker().flush(true);
                                    break;
                                } else {
                                    if (Map.levelMax >= 2) {
                                        popup.show();
                                    } else {
                                        popup1.show();
                                    }
                                }
                            }
                        }
                    }
                    for (int i = 0; i < canvas.getMap().getlArray().length; i++) {
                        for (int j = 0; j < canvas.getMap().getlArray()[i].length; j++) {
                            canvas.getMap().getlArray()[i][j].setState(Button.ENABLE);
                        }
                    }
                    break;
            }
        }
    }

    public void update() {
    }

    public void paint(Graphics g) {
        g.drawImage(canvas.getResource().getIMG(Resource.IMG_BG_CL), 0, 0, Graphics.TOP | Graphics.LEFT);
        canvas.getMap().drawLevelMap(g);
        popup.draw(g);
        popup1.draw(g);
    }

    public void onClickPositiveButton(int id) {
        switch (id) {
            case 1:
                popup.dismiss();
                MIDletGame.purchaseNextLevel();
                break;
            case 2:
                popup1.dismiss();
                break;
        }
    }

    public void onClickNegativeButton(int id) {
        switch (id) {
            case 1:
                popup.dismiss();
                break;
            case 2:
                popup1.dismiss();
                break;
        }
    }
}
